<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 商家消息管理
 * Class Merchant
 * @author heyu
 */
class Merchant extends MY_Controller {
    /**
     * @var merchantMessageModel
     */
    public $mMessage;

    /**
     * @var MerchantUserModel
     */
    public $mMerchant;

    public function __construct(){
        parent::__construct();
        $this->loadModel('merchantMessageModel', 'mMessage', 'merchant');
    }

    /**
     * 首页
     */
    public function index(){
        if ( ! adminAuthority('merchant-add', 'message')){
            redirect(getUrl('message/merchant/history'));
        }
        $data = array(
            'title' => '消息推送'
        );
        $this->view('message/merchant/index', $data, 'datatable,validation,uniform,ueditor');
    }

    /**
     * 获取用户列表
     */
    public function getMerchantList(){
        $this->loadModel('MerchantUserModel', 'mMerchant', 'merchant');
        $data = $this->mMerchant->adminGetList();
        foreach ($data['data'] as $k => $v){
            $img = isset($v['portrait']) ? getImage($v['portrait'], 'xs') : getDefaultPortrait(2);
            $data['aaData'][$k][] = '<input type="checkbox" class="checkboxes" name="merchant_id[]" value="' . $v['id'] .'">';
            $data['aaData'][$k][] = '<img width="100%" src="'.$img.'"  class="img-wh"/>';
//            $data['aaData'][$k][] = isset($v['rank']) ? $this->getRank($v['rank']) : '';
            $data['aaData'][$k][] = $v['shop_name'];
            $data['aaData'][$k][] = $v['mobile'];
            $data['aaData'][$k][] = $v['business'] == 2 ? '营业中' : '休息';
            $data['aaData'][$k][] = $this->adminStatus($v['status'], true);
            $data['aaData'][$k][] = '';
        }
        unset($data['data']);
        echo json_encode($data);
    }

    /**
     * 保存推送
     */
    public function save(){
        //表单验证
        $vConf = array(
            array('type', '类型', 'required|in_list[1,2]'),
            array('title', '标题', 'required'),
            array('content', '简介', 'required'),
            array('content_detail', '详细内容', 'required'),
        );
        $this->formDataValidation($vConf, array('id' => get('id')));
        $type = (int)post('type');
        $title = post('title');
        $content = post('content');
        $data = array();
        $time = time();
        $jPushCode = array();
        if ($type == 1){
            $data[] = array(
                'merchant_id' => 0,
                'type' => 1,
                'title' => $title,
                'content' => trim($content),
                'content_detail' => trim(post('content_detail')),
                'addtime' => $time,
            );
        }else if ($type == 2) {
            $id = post('merchant_ids');
//            $ids = (int)post('ids');
            $merchantId = explode(',', $id);
            if (empty($merchantId)) {
                $this->ajaxReturn(0, '请选择要推送的用户1');
            }
            $ids = array();
            foreach ($merchantId as $k => $v) {
                if (isset($v) && $v > 0) {
                    $data[] = array(
                        'merchant_id' => $v,
                        'type' => 2,
                        'title' => $title,
                        'content' => $content,
                        'content_detail' => trim(post('content_detail')),
                        'addtime' => $time,
                    );
                    $ids[] = $v;
                }
            }

            $this->loadModel('MerchantUserModel', 'mMerchant', 'merchant');

            $merchant = $this->mMerchant->getInfoByIds($ids);
            foreach ($merchant as $c){
                $jPushCode[] = $c['jpush_code'];
            }
        }
        if (empty($data)){
            $this->ajaxReturn(0, '请选择要推送的用户2');
        }
        $tA = $this->mMessage->batchSave($data);
        if (!$tA){
            $this->ajaxReturn(0, '推送失败');
        }
        $contentType = $type;
        $type = $type == 1 ? 2 : 1;
        MerchantJpush($title, $content, $type, $jPushCode, $contentType);
        $this->ajaxReturn(1, '推送成功');
    }

    /**
     * 推送历史记录
     */
    public function history(){
        $data = array(
            'type' => $this->mMessage->getType(),
            'title' => '推送历史'
        );
        $this->view('message/merchant/history', $data, 'datatable,datepicker');
    }

    /**
     * 获取推送历史记录
     */
    public function getMessageList(){
        $data = $this->mMessage->adminPcGetList();
        foreach ($data['data'] as $k => $v){
            $data['aaData'][$k][] = $this->mMessage->getType($v['type']);
            $data['aaData'][$k][] = $v['title'];
            $data['aaData'][$k][] = '<textarea readonly class="form-control remaks-item content textarea_height">'.$v['content'].'</textarea>';
            $data['aaData'][$k][] = dateFormat($v['addtime']);
            $data['aaData'][$k][] = $v['type'] == 1 ? '推送所有人' : '推送'.$v['total'].'人';
            if ($v['type'] == 1){
                $data['aaData'][$k][] = '';
            }else{
                $data['aaData'][$k][] = adminCreateBtn(array(
                    'label' => '明细',
                    'attr' => array(
                        'href' => 'javascript:;',
                        'class' => 'btn btn-xs green-stripe default detail-btn',
                        'time' => $v['addtime'],
                        'date_time' => dateFormat($v['addtime']),
                    ),
                    'tag' => 'a',
                    'permissions' => array(
                        'module' => 'message',
                        'key' => 'merchant-view'
                    )
                ));
            }

        }
        unset($data['data']);
        echo json_encode($data);
    }

    /**
     * 获取同一时间消息推送人
     */
    public function getMerchantDetail(){
        $data = $this->mMessage->getMerchantDetail();
        foreach ($data['data'] as $k => $v){
            $portrait = isset($v['portrait']) ? getImage($v['portrait'], 'xs') : getDefaultImg();
            $data['aaData'][$k][] = '<img width="100%" src="'.$portrait.'" class="portrait img-wh"/>';
            $data['aaData'][$k][] = $v['nickname'];
            $data['aaData'][$k][] = $v['mobile'];
            $data['aaData'][$k][] = '';
        }
        unset($data['data']);
        echo json_encode($data);
    }

    /**
     * 消息类型
     * @param $key
     * @return mixed|string
     */
    public function getRank($key = 0){
        $config = array(
            1 => '初级',
            2 => '中级',
            3 => '高级',
        );
        if( ! $key){
            return $config;
        }
        return $config[$key];
    }
}
